from decorator import decorator
from pylons import session, config
from mtp import model
from pylons.controllers.util import abort

def needs_perm(thing, action):
    def decor(func, *args, **kwargs):
        user = session.get('user')
        if user and user.username == config['mtp.config']['auth']['admin_user']:
            pass
        elif not model.UserPermission.permission(user, thing, action):
            if user:
                abort(403)
            abort(401)

        return func(*args, **kwargs)
    return decorator(decor)


def has_perm(thing, action):
    # TODO cache?
    user = session.get('user')
    if user and user.username == config['mtp.config']['auth']['admin_user']:
        return True
    r =  model.UserPermission.permission(user, thing, action)
    return r 




